package com.lei.web;


import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.lei.dao.ProductDao;
import com.lei.entity.Product;

public class AjaxServlet extends HttpServlet {

	private static final long serialVersionUID = 1L;
	
	@Override
	protected void service(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		
		request.setCharacterEncoding("UTF-8");

		String id=new String(request.getParameter("id").getBytes("ISO8859-1"),"UTF-8");
		String name=request.getParameter("name");
		float price=Float.parseFloat(request.getParameter("price"));
		
		Product pro=new Product();
		ProductDao pd=new ProductDao();
		
		pro.setId(id);
		pro.setName(name);
		pro.setPrice(price);
		pro.setShangjia(true);
		pd.save(pro);
		
		List<Product> list=pd.findAll();
		
		PrintWriter out=response.getWriter();
		out.print("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
		out.print("<product>");
		for(Product p:list){
			out.print("<bianhao>"+p.getId()+"</bianhao>");
			out.print("<name>"+p.getName()+"</name>");
			out.print("<price>"+p.getPrice()+"</price>");		
			out.print("<shangjia>"+p.isShangjia()+"</shangjia>");
		}
		out.print("</product>");
		out.flush();
		out.close();
		
	}

}
